﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace Common
{
    //缓存写入  
    //验证缓存是否存在  
    //缓存读取   按缓存索引读取相应缓存值  
    public static class CacheExtension
    {
        //private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");

        /// <summary>  
        /// 获取当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="cacheKey"></param>  
        /// <returns></returns>  
        public static object GetCache(string cacheKey)
        {
            var objCache = HttpRuntime.Cache;
            return objCache[cacheKey];
        }
        /// <summary>  
        /// 查询指定CacheKey的Cache值是否存在  
        /// </summary>  
        /// <param name="cacheKey"></param>  
        /// <returns></returns>  
        public static bool IsExistsCache(string cacheKey)
        {
            var objCache = HttpRuntime.Cache;
            return objCache[cacheKey] != null;
        }

        /// <summary>  
        /// 设置当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="cacheKey"></param>  
        /// <param name="objObject"></param>  
        public static void SetCache(string cacheKey, object objObject)
        {
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject);
        }

        /// <summary>  
        /// 设置当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="cacheKey"></param>  
        /// <param name="objObject"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="slidingExpiration"></param>  
        public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }
        /// <summary>  
        /// 设置数据缓存  
        /// </summary>  
        public static void SetCache(string cacheKey, object objObject, TimeSpan timeout)
        {
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
        }
        /// <summary>  
        /// 移除指定数据缓存  
        /// </summary>  
        public static void RemoveAllCache(string cacheKey)
        {
            var cache = HttpRuntime.Cache;
            cache.Remove(cacheKey);
        }
        /// <summary>  
        /// 移除全部缓存  
        /// </summary>  
        public static void RemoveAllCache()
        {
            var cache = HttpRuntime.Cache;
            var cacheEnum = cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cache.Remove(cacheEnum.Key.ToString());
            }
        }



        private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");
        private const string ApplicationCacheKeyFormat = "Application_{0}";
        private const string UserInfoCacheKeyFormat = "UserInfo_{0}";
        private const string RolePopedomCacheKeyFormat = "RolePopedom_{0}";

        public static void InsertForSlidingExpiration(this Cache cache, string key, object value)
        {
            cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(CacheMinute));
        }

        public static T Get<T>(this Cache cache, object dataCacheKey, Func<object[], T> getService) where T : class
        {
            var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
            var data = cache.Get(cacheKey) as T;
            if (data != null)
            {
                return data;
            }
            data = getService(new object[] { dataCacheKey });
            if (data != null)
            {
                cache.InsertForSlidingExpiration(cacheKey, data);
                return data;
            }
            return null;
        }

        public static void Remove<T>(this Cache cache, object dataCacheKey)
        {
            var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
            cache.Remove(cacheKey);
        }
    }
}
